package com.contory.ssn_lgnd.app.maingame;

import android.util.Log;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.comp.DrawPtr;
import com.contory.ssn_lgnd.app.comp.UpdatePtr;
import com.contory.ssn_lgnd.app.comp.ViewObject;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class PlayerInfoGUI implements UpdatePtr, DrawPtr{
	ViewObject bodyView;
	BarGraphView energyBar;
	Vector2 fontHPPos;
	Vector2 fontWPPos;
	float centerRate = 0.3f;
	WPSelector wp;
	final Player player;
	final Controler con;
	
	public PlayerInfoGUI(float x, float y, float w, float h, Player player, Controler con){
		bodyView = new ViewObject();
		bodyView.setXY_WH_TR(x, y, w, h, Assets.TEX_REG_BOARD);
		//bodyView.setRGBA(0.64f, 0.29f, 0.64f, 0.75f);
		
		float padding = -0.25f;
		energyBar = new BarGraphView(x + w/4 + padding, y + h/4, w/2, h/3.2f, player.getEnergy());
		wp = new WPSelector(x + w/4 + padding, y - h/4, w/2, h/3);
		
		fontHPPos = new Vector2(x - w/4 + padding / 2, y + h/4);
		fontWPPos = new Vector2(x - w/4 + padding / 2, y - h/4);
		
		this.player = player;
		this.con = con;
	}
	
	public void setEnergy(int energy){
		energyBar.setBarCellCount(energy);
	}
	
	@Override
	public void draw(SpriteBatcher batcher) {
		batcher.drawSprite(bodyView.x, bodyView.y, bodyView.w, bodyView.h, 
				bodyView.r, bodyView.g, bodyView.b, bodyView.a, bodyView.tr);
		energyBar.draw(batcher);
		Assets.font.drawText(batcher, "HP", fontHPPos.x, fontHPPos.y, energyBar.bodyView.h, energyBar.bodyView.h, 1, 1, 1, 1);
		
		wp.draw(batcher);
		Assets.font.drawText(batcher, "WP", fontWPPos.x, fontWPPos.y, energyBar.bodyView.h, energyBar.bodyView.h, 1, 1, 1, 1);
	}

	@Override
	public void update(float deltaTime) {
		if(con.getSwapBool() == Controler.RIGHT){
			wp.rightShift();
		}
		energyBar.setBarCellCount(player.getEnergy());
	}
	
	public int getCurWP(){
		return wp.getCurWP();
	}
	
	class WPSelector implements DrawPtr { //UpdatePtr, 
		public static final int RED = 0;
		public static final int YELLO = 1;
		
		static final float CELL_VIEW_W = 1.0f;
		static final float CELL_VIEW_H = 1.0f;
		ViewObject redWP;
		ViewObject yelloWP;
		ViewObject cursor;
		int curPtr = 0;
		float[] xPosArr;
		int dirX = 0;
		
		public WPSelector(float x, float y, float w, float h){
			xPosArr = new float[2];
			redWP = new ViewObject();
			redWP.setXY_WH_TR(x - w/4, y, CELL_VIEW_H, h, Assets.tex_reg_redBullet);
			xPosArr[0] = redWP.x;
			yelloWP = new ViewObject();
			yelloWP.setXY_WH_TR(x + w/4, y, redWP.w, redWP.h, Assets.TEX_REG_YELLOBULLET);
			xPosArr[1] = yelloWP.x;
			cursor = new ViewObject();
			cursor.setXY_WH_TR(redWP.x, redWP.y, CELL_VIEW_W, CELL_VIEW_H, Assets.TEX_REG_CURSOR);
		}

		@Override
		public void draw(SpriteBatcher batcher) {
			batcher.drawSprite(redWP.x, redWP.y, redWP.w, redWP.h, redWP.tr);
			batcher.drawSprite(yelloWP.x, yelloWP.y, yelloWP.w, yelloWP.h, yelloWP.tr);
			batcher.drawSprite(cursor.x, cursor.y, cursor.w, cursor.h, cursor.tr);
		}
		
		public void rightShift(){
			curPtr++;
			if(curPtr > xPosArr.length - 1){
				curPtr = 0;
			}
			cursor.x = xPosArr[curPtr];
		}
		
		public int getCurWP(){
			return curPtr;
		}
	}
}
